// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    motto: 'Hello World',
    bannerList: [],
    newsList: [],
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    active: 0
  },
  methods: {
    // 事件处理函数
    menuChange(e: any) {
      // @ts-ignore
      this.setData({active: e.detail});
      switch (e.detail) {
        case 1:
          wx.navigateTo({
            url: `../lineup/lineup`
          })
          break;
        case 2:
          wx.navigateTo({
            url: `../my/my`
          })
          break;
      }
    },
    bannerData() {
      wx.request({
        url: 'https://wxapi.db101.cn/v1/jcc/index/banner/list',
        success: (res) => {
          // @ts-ignore
          this.setData({bannerList: res.data.data})
        }
      })
    },
    newsData() {
      wx.request({
        url: 'https://wxapi.db101.cn/v1/jcc/index/news/list',
        success: (res) => {
          // @ts-ignore
          this.setData({newsList: res.data.data})
        }
      })
    },
    serverLogin() {
      wx.login({
        success: (res) => {
          wx.request({
            url: 'https://wxapi.db101.cn/v1/jcc/wxLogin',
            method: 'POST',
            data: {
              code: res.code
            },
            success: (res) => {
              console.log(res)
              // 存储用户数据到本地存储
              wx.setStorageSync('user', res.data.data);
            }
          })
        }
      })
    },
    openLineupList(e: any) {
      // @ts-ignore
      wx.navigateTo({
        url: `/pages/lineup-list/lineup-list?id=${e.target.dataset.id}`,
      })
    }
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      // @ts-ignore
      this.bannerData();
      // @ts-ignore
      this.newsData();
      // 获取本地用户数据
      const user = wx.getStorageSync('user');
      // 判断用户数据是否为空
      if (!user) {
        // @ts-ignore
        this.serverLogin();
      }
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  }
})
